package com.zaofan.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;

import com.zaofan.bean.ExpressBuilder;
import com.zaofan.dao.ExpressBuilderDao;
import com.zaofan.utils.C3p0DataSourceUtils;

public class ExpressBuilderService {

	private ExpressBuilderDao expressBuilderDao = new ExpressBuilderDao();

	public void add(ExpressBuilder expressBuilder) {
		expressBuilderDao.add(expressBuilder);
	}

	public void del(ExpressBuilder expressBuilder) {
		expressBuilderDao.del(expressBuilder);
	}

	public void delByBuilderId(int builderId) {
		try {
			String sql = "delete from expressbuilder where builderId = "
					+ builderId;
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(
					C3p0DataSourceUtils.getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void delByExpressId(int expressId) {
		try {
			String sql = "delete from expressbuilder where expressId = "
					+ expressId;
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(
					C3p0DataSourceUtils.getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<ExpressBuilder> findByExpressId(int expressId) {
		Map<String, Object> params = new HashMap<>();
		params.put("expressId", expressId + "");
		List<ExpressBuilder> data = expressBuilderDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public ExpressBuilder findByBuilderId(int builderId) {
		Map<String, Object> params = new HashMap<>();
		params.put("builderId", builderId + "");
		ExpressBuilder data = expressBuilderDao.findOne(params);

		return data;
	}

	public String findByExpressIdBuilderToIds(int expressId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("expressId", expressId + "");
		List<ExpressBuilder> areaIds = expressBuilderDao.findAll(params);
		if (areaIds == null || areaIds.size() == 0) {
			return "";
		}
		String ids = "";
		for (ExpressBuilder builder : areaIds) {
			ids += builder.getBuilderId() + ",";
		}
		if(!"".equals(ids)){
			ids = ids.substring(0, ids.length() - 1);
		}
		return ids;
	}

	public Long count(Map<String, Object> params) {
		return expressBuilderDao.count(params);
	}

	public ExpressBuilder findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return expressBuilderDao.findOne(params);
	}

}
